package com.stone.service;


import com.stone.entity.Stock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 库存服务类
 * @author: zoubaolu
 * @date: 2022年06月23日 9:30
 */
@Service
public class StockService {

    @Value("${server.port}")
    private String serverPort;

    public String descStock(String goodsId, Integer num) {
        List<Stock> list = new ArrayList<>();
        list.add(new Stock(1, 10, "001", "商品 A, port:" + serverPort, "5X6"));
        list.add(new Stock(2, 8, "002", "商品 B, port:" + serverPort, "6X6"));
        list.add(new Stock(3, 6, "003", "商品 C, port:" + serverPort, "7X6"));
        list.add(new Stock(4, 13, "004", "商品 D, port:" + serverPort, "4X6"));
        list.add(new Stock(5, 18, "005", "商品 E, port:" + serverPort, "2X6"));
        Stock stock = list.stream().filter(s -> goodsId.equals(s.getGoodsId())).findFirst().get();
        String msg;
        if (stock.getQuantity() >= num) {
            msg = "下单成功，剩余库存：" + (stock.getQuantity() - num) + "；port：" + serverPort;
        } else {
            msg = "下单失败，库存不足" + "；port：" + serverPort;
        }
        System.out.println(msg);
        return msg;
    }
}
